package com.ycxy.sh.transaction.service.impl;

import com.ycxy.sh.transaction.dao.entity.*;
import com.ycxy.sh.transaction.dao.vo.LayuiPageVo;
import com.ycxy.sh.transaction.dao.vo.UserInfoStandardVo;
import com.ycxy.sh.transaction.ex.ServiceException;
import com.ycxy.sh.transaction.mapper.*;
import com.ycxy.sh.transaction.service.AdminService;
import com.ycxy.sh.transaction.util.KeyUtil;
import com.ycxy.sh.transaction.web.JsonResult;
import com.ycxy.sh.transaction.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 宋H
 * @Date: 2023/3/29
 * @Description: com.ycxy.sh.transaction.service.impl
 * @version: 1.0
 */
@Service
@Slf4j
public class AdminServiceImpl implements AdminService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private NoticesMapper noticesMapper;

    @Autowired
    private CommodityMapper commodityMapper;

    /**
     * 设置为管理员或普通用户
     * @param userid
     * @param roleid
     */
    @Override
    public void setUserType(Long userid, Integer roleid) {
        log.debug("开始处理设置为管理员或普通用户的业务...");
        if (roleid == 2){
            int i = loginMapper.updateLogin(new Login().setUserId(userid).setRoleId(roleid));
            if (i != 1){
                String message="设置管理员失败！";
                log.debug(message);
                throw  new ServiceException(ServiceCode.ERR_UPDATE,message);
            }
            userRoleMapper.Update(new UserRole().setUserId(userid).setRoleId(2).setIdentity("网站管理员"));
            /**发出设置为管理员的系统通知*/
            Notices notices = new Notices().setNoticeId(Long.valueOf(KeyUtil.genUniqueKey())).setUserId(userid).setNoticeType("系统通知")
                    .setNoticeContent("恭喜您已被设置为网站管理员，努力维护网站的良好氛围。");
            noticesMapper.insert(notices);
            log.debug("已成功设置为管理员用户");
        }else if (roleid == 1){
            Integer i = loginMapper.updateLogin(new Login().setUserId(userid).setRoleId(roleid));
            if (i != 1){
                String message="设置为普通用户失败！";
                log.debug(message);
                throw  new ServiceException(ServiceCode.ERR_UPDATE,message);
            }
            userRoleMapper.Update(new UserRole().setUserId(userid).setRoleId(1).setIdentity("网站用户"));
            /**发出设置为网站用户的系统通知*/
            Notices notices = new Notices().setNoticeId(Long.valueOf(KeyUtil.genUniqueKey())).setUserId(userid).setNoticeType("系统通知")
                    .setNoticeContent("您已被设置为网站用户，希望您再接再厉。");
            noticesMapper.insert(notices);
            log.debug("已成功设置为网站普通用户");
        }
        throw new ServiceException(ServiceCode.ERR_FORBIDDEN,"操作失败,您的权限不够");
    }

    /**
     * 根据用户id账号状态（userstatus）来修改用户的账号状态
     * @param userid
     * @param userstatus
     */
    @Override
    public void adminUserStatus(Long userid, Integer userstatus) {
        log.debug("开始处理账号解封或封号处理的业务...");
        if (userstatus == 0){
            Integer i = loginMapper.updateLogin(new Login().setUserId(userid).setUserStatus(userstatus));
            Integer j = userInfoMapper.Update(new UserInfo().setUserId(userid).setUserStatus(userstatus));
            if (i < 1 || j < 1){
                String message="封号失败";
                log.debug(message);
                throw  new ServiceException(ServiceCode.ERR_UPDATE,message);
            }
            /**发出封号的系统通知*/
            Notices notices = new Notices().setNoticeId(Long.valueOf(KeyUtil.genUniqueKey())).setUserId(userid).setNoticeType("系统通知")
                    .setNoticeContent("因为您的不良行为，您在该网站的账号已被封号。");
            noticesMapper.insert(notices);
            log.debug("封号成功");
        }else if (userstatus == 1){
            Integer i = loginMapper.updateLogin(new Login().setUserId(userid).setUserStatus(userstatus));
            Integer j = userInfoMapper.Update(new UserInfo().setUserId(userid).setUserStatus(userstatus));
            if (i <1 || j < 1){
                String message="解封失败";
                log.debug(message);
                throw  new ServiceException(ServiceCode.ERR_UPDATE,message);
            }
            /**发出解封的系统通知*/
            Notices notices = new Notices().setNoticeId(Long.valueOf(KeyUtil.genUniqueKey())).setUserId(userid).setNoticeType("系统通知")
                    .setNoticeContent("您在该网站的账号已被解封，希望您保持良好的行为。");
            noticesMapper.insert(notices);
            log.debug("解封成功");
        }
        throw  new ServiceException(ServiceCode.ERR_UPDATE,"操作失败,您的权限不够");
    }

    @Override
    public LayuiPageVo pageAdminCommodity(Integer commodityStatus, int limit, int page) {
        log.debug("分页管理员查看各类商品信息的业务...");
        if(commodityStatus==100){
            List<Commodity> commodityList = commodityMapper.queryAllCommodity((page - 1) * limit, limit, null, null);
            Integer dataNumber = commodityMapper.queryCommodityCount(null, null);
            return new LayuiPageVo("",0,dataNumber,commodityList);
        }else{
            List<Commodity> commodityList = commodityMapper.queryAllCommodity((page - 1) * limit, limit, null, commodityStatus);
            Integer dataNumber = commodityMapper.queryCommodityCount(null, commodityStatus);
            return new LayuiPageVo("",0,dataNumber,commodityList);
        }
    }

    /**
     * 管理员对商品的审核
     * @param commodityId
     * @param commodityStatus
     */
    @Override
    public void ChangeCommodityStatus(Long commodityId, Integer commodityStatus) {
        log.debug("开始处理管理员对商品的审核的业务...");
        Integer i = commodityMapper.ChangeCommstatus(commodityId, commodityStatus);
        if (i != 1){
            String message="审核商品失败";
            log.debug(message);
            throw  new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        /**根据商品id发出商品审核结果的系统通知*/
        Commodity commodity = commodityMapper.selectById(new Commodity().setCommodityId(commodityId));
        if (commodityStatus == 0){
            Notices notices = new Notices().setNoticeId(Long.valueOf(KeyUtil.genUniqueKey())).setUserId(commodity.getUserId()).setNoticeType("商品审核")
                    .setNoticeContent("您的商品 <a href=/product-detail/"+commodity.getCommodityId()+" style=\"color:#08bf91\" target=\"_blank\" >"+commodity.getCommodityName()+"</a> 未通过审核，目前不支持公开发布。");
            noticesMapper.insert(notices);
            log.debug("通知插入成功");
        }else if (commodityStatus == 1){
            Notices notices = new Notices().setId(Long.valueOf(KeyUtil.genUniqueKey())).setUserId(commodity.getUserId()).setNoticeType("商品审核")
                    .setNoticeContent("您的商品 <a href=/product-detail/"+commodity.getCommodityName()+" style=\"color:#08bf91\" target=\"_blank\" >"+commodity.getCommodityName()+"</a> 已通过审核，快去看看吧。");
            noticesMapper.insert(notices);
        }
       log.debug("审核商品成功");
    }

    /**
     * 分页查询不同角色用户信息
     * @param page
     * @param roleid
     * @param userstatus
     * @return
     */
    @Override
    public LayuiPageVo pageUserInfo(int limit, int page,Integer roleid ,Integer userstatus) {
        List<UserInfoStandardVo> userInfoStandardVoList = userInfoMapper.pageAllUserInfo((page - 1) * limit, limit,roleid,userstatus);
        Integer dataNumber = userInfoMapper.Count(roleid);
        return new LayuiPageVo("",0,dataNumber,userInfoStandardVoList);
    }
}
